<?php
/**
 * 初中生成绩统计
 * 
 * @version 1.0
 * @author jinyue.wang
 * @date 2013-12-15
 */

class DefaultController extends FController {

    public function accessRules() {
        $config = array(
            'index' => 1
        );
        return ZRole::auth("statistics", $config);
    }

    public function __common() {
        parent::init();
        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        }

        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }

    public function actionIndex() {
        
        $this->__common();
        
        $zExam = new zExam();
        $examArray = $zExam->_getExamObject();
        
        $zEdu = new ZEdu();  
        $schoolObject = $zEdu->_getSchoolInfo();
        
        $this->title = '(初中)成绩统计';
        $this->render('index',array(
            'examArray' => $examArray,
            'schoolObject' => $schoolObject
        ));
    }

    public function actionList(){
        $this->__common();
        $this->layout = "//layouts/main_other";
        
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/Highcharts/js/highcharts.js");
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/Highcharts/js/modules/exporting.js");

        $exam_id = $this->request->getParam('exam_id');
        $school_id = $this->request->getParam('school_id');
        
        //得到当前考试分数标准
        $zExam = new zExam();
        $examArray = $zExam->_getSubArray($exam_id);
        
        $zEdu = new ZEdu();
        $subjectObject = $zEdu->_getSubjectObject();
        
        $num = 0;
        
        $subject_array = array();
        
        $list2 = '';
        $examInfo = $zExam->_getExamInfo($exam_id);
        if(!empty($examInfo)){
            $subject_ids = $examInfo['exam_subject_ids'];
            if(!empty($subject_ids)){
                $sAr = explode('-', $subject_ids);
                foreach($sAr as $s){
                    $subject_array[$s] = $subjectObject[$s]['subject_name'];
                    
                    $list2 .= '<th width="88" align="center" class="border_d">'.$subject_array[$s].'</th>
                    <th width="87" align="center" class="border_d"></th>
                    <th width="87" align="center" class="border_d"></th>
                    <th width="87" align="center" class="border_d"></th>';
                }
                $num = count($subject_array);
            }
        }
        
        $table = array();
        
        $list1 = '<th width="88" align="center" class="border_d"></th>
                    <th width="87" align="center" class="border_d"></th>
                    <th width="87" align="center" class="border_d"></th>
                    <th width="87" align="center" class="border_d"></th>';
        
        $list3 = '<td align="center" class="border_d">平均分</td>
                    <td align="center" class="border_d">优秀率</td>
                    <td align="center" class="border_d">及格率</td>
                    <td align="center" class="border_d">低分率</td>';
        for($i=1;$i<$num;$i++){
            $list1 .= $list1;
            $list3 .= $list3;
        }
        
        $table[0] = $list1;
        $table[1] = $list2;
        $table[2] = $list3;

        $data = array();
        
        //得到当前考试人数分数
        $point = $zExam->_getExamArray($school_id,$exam_id,$examArray);
        if(!empty($point)){
            
            $subject_array = $new_data = array();
            foreach($point as $key => $value){
                $new_data['people'] = $value['people'];
                $new_data['point'] = $value['point'];
                $type1 = $type2 = $type3 = 0;
                foreach($value['student'] as $sKey => $sVal){
                    $subject_array[$sKey]['point'] = $sVal;
                    $subject_array[$sKey]['type1'] = isset($value['subject'][$sKey][1])?$value['subject'][$sKey][1]:0;
                    $subject_array[$sKey]['type2'] = isset($value['subject'][$sKey][2])?$value['subject'][$sKey][2]:0;
                    $subject_array[$sKey]['type3'] = isset($value['subject'][$sKey][3])?$value['subject'][$sKey][3]:0;
                    
                    $type1 += $subject_array[$sKey]['type1'];
                    $type2 += $subject_array[$sKey]['type2'];
                    $type3 += $subject_array[$sKey]['type3'];
                }
                $new_data['type1'] = $type1;
                $new_data['type2'] = $type2;
                $new_data['type3'] = $type3;
                $new_data['subject'] = $subject_array;
                $data[$key] = $new_data;
            }
        }

        $schoolObject = $zEdu->_getSchoolObject();

        $this->title = '(初中)成绩统计';
        $this->render('list',array(
            'point'         => $data,
            'examInfo'      => $examInfo,
            'table'         => $table,
            'subjectObject'    => $subjectObject,
            'schoolObject'  => $schoolObject
        ));
    }
    
    public function actionInterval(){
        
        $this->__common();
        $this->layout = "//layouts/main_other";
        
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/Highcharts/js/highcharts.js");
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/Highcharts/js/modules/exporting.js");

        $exam_id = $this->request->getParam('exam_id');
        $school_id = $this->request->getParam('school_id');
        
        $score_duan = $scoreArray = array();
        
        $zedu = new ZEdu();
        
        //得到当前考试分数标准
        $zExam = new zExam();
        $examArray = $zExam->_getSectionObject($exam_id);
        if(!empty($examArray)){
            $scoreArray = $zedu->_getScoreArray($examArray['score_type1'],$examArray['score_type2'],$examArray['score_type3']);
        }

        $subject_array = array();
        $examInfo = $zExam->_getExamInfo($exam_id);

        $list1 = $list2 = '';
        //得到当前考试人数分数
        $point = $zExam->_getExamArrayList($school_id,$exam_id,$scoreArray);
        if(!empty($point)){
            foreach($point['key'] as $val){
                $list1 .= '<th width="87" align="center" class="border_d"></th>';
                $list2 .= '<td align="center" class="border_d">'.$val.'</td>';
            }
        }
        
        $schoolObject = $zedu->_getSchoolObject();
        
        $this->render('interval',array(
            'examArray'     => $examArray,
            'examInfo'      => $examInfo,
            'point'         => $point,
            'list1'          => $list1,
            'list2'          => $list2,
            'schoolObject'  => $schoolObject
        ));
    }
    
}